﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enter.Mvc.Base.Controllers;
using Enter.Services.DataCache;
using Enter.Services.Mail;
using Enter.Entity;
using System.Web.Mvc;
using Enter.Mvc.Mall.Model.Product;
using Enter.Common;
using System.Reflection;

namespace Enter.Mvc.Mall.Controllers
{

    public class CategoryController : BaseControllers
    {
        private readonly ICache icache;
        private readonly IMail imail;

        public CategoryController(ICache cache, IMail mail)
        {
            this.icache = cache;
            this.imail = mail;
        }
        #region 全部商品分类页
        public ViewResult iwanttobuy(string parentId)
        {
            //获取所有类型
            List<T105_GoodClassEntity> listGood = imail.GetGoodClassList("0");
           List<Model.Category.CategoryModel> parentCates=new List<Model.Category.CategoryModel>();
           if (listGood != null)
           {
               Model.Category.CategoryModel modelall = new Model.Category.CategoryModel();
               if (string.IsNullOrEmpty(parentId) || parentId=="0")
               {
                   modelall.Selected = true;
               }
               modelall.CNO = "0";
               modelall.CName = "所有商品分类";

               parentCates.Add(modelall);

               foreach (T105_GoodClassEntity entity in listGood)
               {
                   Model.Category.CategoryModel model = new Model.Category.CategoryModel();
                   if (parentId == entity.CNO)
                   {
                       model.Selected = true;
                   }
                   else
                   {
                       model.Selected = false;
                   }

                   model.CNO = entity.CNO;
                   model.CName = entity.CName;
                   model.Icon = entity.icon;
                   parentCates.Add(model);
               }
           }
          
            

            //获取商品类型(二级类型)
            List<Model.Category.CategoryModels> CategoryModels = new List<Model.Category.CategoryModels>();
         
            IDictionary<string, object> obj = new Dictionary<string, object>();
            int num = 13;
            if (string.IsNullOrEmpty(parentId) || parentId == "0")
            {
                obj.Add("key", "0");
                parentId = "";
            }
            else
            {
                obj.Add("key", "1");
                num = 7;
            }
            obj.Add("allCate", parentCates);
            CategoryModels = GetSecondCate(parentId, num);
            obj.Add("SecondCateList", CategoryModels);
            return View(obj);
        }

        private List<Model.Category.CategoryModels> GetSecondCate(string parentId,int minCount)
        {
            List<Model.Category.CategoryModels> CategoryModels = new List<Model.Category.CategoryModels>();
            List<T105_GoodClassEntity> SecondCate = imail.GetCateSecondList(parentId);
            if (SecondCate != null)
            {
                foreach (T105_GoodClassEntity item in SecondCate)
                {
                    Model.Category.CategoryModels models = new Model.Category.CategoryModels();
                    Model.Category.CategoryModel model = new Model.Category.CategoryModel();
                    model.CNO = item.CNO;
                    model.CName = item.CName;
                    model.Icon = item.icon;
                    models.cate = model;

                    List<T105_GoodClassEntity> MinCate = imail.GetCateMinList(minCount, item.CNO);

                    List<Model.Category.CategoryModel> MinCateList = new List<Model.Category.CategoryModel>(); ;
                    if (MinCate != null)
                    {
                      
                        foreach (T105_GoodClassEntity items in MinCate)
                        {
                            Model.Category.CategoryModel model1 = new Model.Category.CategoryModel();
                            model1.CNO = items.CNO;
                            model1.CName = items.CName;
                            model1.Icon = items.icon;
                            MinCateList.Add(model1);
                        }
                        models.MinCateList = MinCateList;
                        CategoryModels.Add(models);
                    }
                }

            }
            return CategoryModels;
        }
        #endregion

        #region 商品大分类
        /// <summary>
        /// 商品类型
        /// </summary>
        /// <returns></returns>
        public ActionResult AllProductCate()
        {
            List<T105_GoodClassEntity> all = imail.GetAllGoodClassList();
            List<T105_GoodClassEntity> list = all.FindAll(model => model.ParentID == "0");

            List<CategoryListModel> CategoryList = null;
            if (list != null && list.Count > 0)
            {
                CategoryList = new List<CategoryListModel>();
                foreach (T105_GoodClassEntity entity in list)
                {
                    CategoryListModel model = new CategoryListModel();
                    CategoryModel category = new CategoryModel();
                    category.CNO = entity.CNO;
                    category.CName = entity.CName;
                    model.Category = category;
                    List<CategoryModel> categorylist = null;
                    List<T105_GoodClassEntity> clist = all.FindAll(cmodel => cmodel.ParentID == entity.CNO);
                    if (clist != null && clist.Count > 0)
                    {
                        categorylist = new List<CategoryModel>();
                        foreach (T105_GoodClassEntity centity in clist)
                        {
                            CategoryModel c = new CategoryModel();
                            c.CNO = centity.CNO;
                            c.CName = centity.CName;
                            categorylist.Add(c);
                        }
                    }
                    model.ChildCategorys = categorylist;
                    CategoryList.Add(model);
                }
            }

            return PartialView(CategoryList);

        }
        #endregion

        /// <summary>
        /// 类别导航
        /// </summary>
        /// <param name="cNo"></param>
        /// <returns></returns>
        public ActionResult CategoryTree()
        {

            return Getcate();
        }
        /// <summary>
        /// 类别导航
        /// </summary>
        /// <param name="cNo"></param>
        /// <returns></returns>
        public ActionResult searchbar()
        {

            return Getcate();
        }

        private ActionResult Getcate()
        {
            string cNo = RequestStrHelper.GetStringFromRequest("cat");

            string key = CustomKey.Build(MethodInfo.GetCurrentMethod(), new object[] { cNo });

           

            if (!string.IsNullOrEmpty(cNo))
            {
                icache.Get<Model.Category.CategoryModel>(key);

                Model.Category.CategoryModels models = icache.Get<Model.Category.CategoryModels>(key);

                if (models == null)
                {
                    string farentNo = cNo.Substring(0, 4);
                    T105_GoodClassEntity item = imail.GetCatebyNO(farentNo);
                    if (item != null)
                    {
                        models = new Model.Category.CategoryModels();
                        Model.Category.CategoryModel model = new Model.Category.CategoryModel();
                        model.CNO = item.CNO;
                        model.CName = item.CName;
                        model.Icon = item.icon;
                        models.cate = model;

                        List<T105_GoodClassEntity> MinCate = imail.GetCateMinList(0, item.CNO);

                        List<Model.Category.CategoryModel> MinCateList = new List<Model.Category.CategoryModel>(); ;
                        if (MinCate != null)
                        {

                            foreach (T105_GoodClassEntity items in MinCate)
                            {
                                Model.Category.CategoryModel model1 = new Model.Category.CategoryModel();
                                if (items.CNO == cNo)
                                {

                                    models.CurrentCateName = items.CName;
                                    
                                }
                                model1.CNO = items.CNO;
                                model1.CName = items.CName;
                                model1.Icon = items.icon;
                                MinCateList.Add(model1);
                            }
                            models.MinCateList = MinCateList;

                        }
                        icache.Put(key, models);
                    }
                }
                return PartialView(models);
            }
            return PartialView(null);
        }
    }
}
